package com.zzq.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzq.guli.service.base.result.R;
import com.zzq.guli.service.edu.entity.Chapter;
import com.zzq.guli.service.edu.entity.ChapterVo;
import com.zzq.guli.service.edu.entity.Video;
import com.zzq.guli.service.edu.mapper.ChapterMapper;
import com.zzq.guli.service.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzq.guli.service.edu.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ZZQ
 * @since 2024-04-12
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {


    @Autowired
    ChapterService chapterService;

    @Autowired
    VideoService videoService;
    @Override
    public List<ChapterVo> getNestedList(String courseId) {
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        chapterQueryWrapper.orderByAsc("sort", "id");
        List<Chapter> chapterList = chapterService.list(chapterQueryWrapper);

        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        videoQueryWrapper.orderByAsc("sort", "id");
        List<Video> videoList =videoService.list(videoQueryWrapper);




        List<ChapterVo> chapterVoList=chapterList.stream().map(chapter -> {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            return chapterVo;
        }).collect(Collectors.toList());


        chapterVoList.forEach(chapterVo -> {
            List<Video> videos = videoList.stream().filter(video -> Objects.equals(video.getChapterId(), chapterVo.getId()))
                    .collect(Collectors.toList());
            chapterVo.setChildren(videos);
        });

        return chapterVoList;

    }
}

